package com.github.pig.admin.controller.notificationInfo;

import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.model.zhgl.notification.NotificationInfo;
import com.github.pig.admin.service.notificationInfo.NotificationInfoService;
import com.github.pig.admin.service.notificationInfo.NotificationUserService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 通知公告 前端控制器
 * </p>
 *
 * @author xiaosa
 * @since 2018-07-05
 */
@RestController
@RequestMapping("/notificationInfo")
public class NotificationInfoController extends BaseController {

    @Autowired private NotificationInfoService notificationInfoService;
    @Autowired private NotificationUserService notificationUserService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return NotificationInfo
    */
    @GetMapping("/{id}")
    public NotificationInfo get(@PathVariable String id) {
        return notificationInfoService.selectById(id);
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/index/page")
    public Page page(@RequestParam Map<String, Object> params,NotificationInfo notificationInfo) {
        Page<NotificationInfo> page = new Page<>(Integer.parseInt(params.get("page").toString()),
                Integer.parseInt(params.get("limit").toString()));
        return notificationInfoService.indexPage(page,notificationInfo);
    }


    /**
     * 添加
     * @param  notificationInfo  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody NotificationInfo notificationInfo) {
        return new R<>(notificationInfoService.insert(notificationInfo));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        NotificationInfo notificationInfo = new NotificationInfo();
        notificationInfo.setId(id);
        notificationInfo.setUpdateDate(new Date());
        notificationInfo.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(notificationInfoService.updateById(notificationInfo));
    }

    /**
     * 编辑
     * @param  notificationInfo  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody NotificationInfo notificationInfo) {
        notificationInfo.setUpdateDate(new Date());
        return new R<>(notificationInfoService.updateById(notificationInfo));
    }
}
